
import UIKit
import MJRefresh
import Eelay



public protocol MoPageHeader{
    var refreshBlock:()->Void{get set}
    func beginRefresh()
    func endRefreshing()
    func setScrollView(scrollView:UIScrollView)
}

public protocol MoPageFooter{
    var loadBlock:()->Void{get set}
    func beginLoad()
    func endLoad()
    func setScrollView(scrollView:UIScrollView)
    func resetNoMoreData()
    func endLoadWithNoMoreData()
}


public class MJRefreshHead:NSObject,MoPageHeader{
    public func beginRefresh() {
        mj_header?.beginRefreshing()
    }
    
    public var refreshBlock: () -> Void = {}
    
    public override init() {}
    
    public func endRefreshing() {
        mj_header?.endRefreshing()
    }
    
    
    public var mj_header:MJRefreshHeader? = nil
    public func setScrollView(scrollView: UIScrollView) {
        let header = MJRefreshHeader {[weak self] in
            self?.refreshBlock()
        }
        header.tag = UIScrollView.tag_paginator_header
        scrollView.mj_header = header
        self.mj_header = header

    }
    
}

public class MJRefreshFoot:NSObject,MoPageFooter{
    public func endLoadWithNoMoreData() {
        mj_footer?.endRefreshingWithNoMoreData()
    }
    
    public func beginLoad() {
        mj_footer?.beginRefreshing()
    }
    
    public var loadBlock: () -> Void = {}
    
    public func endLoad() {
        mj_footer?.endRefreshing()
    }
    
    
    public func resetNoMoreData() {
        mj_footer?.resetNoMoreData()
    }
    
    public override init() {
        
    }

    
    var mj_footer:MJRefreshAutoNormalFooter? = nil
    public func setScrollView(scrollView: UIScrollView) {
        let footer = MJRefreshAutoNormalFooter(refreshingBlock: {[weak self] in
            self?.loadBlock()
        })
        _ = footer.stateLabel?.ui.font14.text(color: ._foregroundWeak)
        _ = footer.stateLabel?.ui.font12.text(color: ._foregroundWeak)
        footer.isRefreshingTitleHidden = true
        footer.isRefreshingTitleHidden = true
        footer.setTitle("  ", for: .noMoreData)
        footer.setTitle("  ", for: .pulling)
        footer.setTitle("  ", for: .willRefresh)
        footer.setTitle("  ", for: .refreshing)
        footer.setTitle("  ", for: .idle)
        footer.loadingView?.style = MoTheme.global.mode.indicatorStyle
//        if #available(iOS 13.0, *) {
//            footer.loadingView?.style = .medium
//        } else {
//            // Fallback on earlier versions
//        }
        scrollView.mj_footer = footer
        self.mj_footer = footer
    }
}

open class UIRefreshHead:MoPageHeader{
    public var refreshBlock: () -> Void = {}
    
    public func beginRefresh() {
    }
        

    public func endRefreshing() {
        refreshControl.endRefreshing()
    }
    public var initStyle:(_:UIRefreshHead)->Void = {header in
        
    }
    
    
    
    public let refreshControl = UIRefreshControl()
    public init() {
        refreshControl.tintColor = ._foregroundWeak
    
        refreshControl.addTarget(self, action: #selector(triggerRefresh), for: .valueChanged)
        initStyle(self)
    }
    
    public func setScrollView(scrollView: UIScrollView) {
        refreshControl.tag = UIScrollView.tag_paginator_header
        scrollView.addSubview(refreshControl)
    }
        
    @objc public func triggerRefresh(){
        if refreshControl.isRefreshing
        {
            refreshBlock()
        }
    }
}

public class MoRefreshHead:NSObject,MoPageHeader{
    public var refreshBlock: () -> Void = {}
    
    public func beginRefresh() {
        mj_header?.beginRefreshing()
    }
    
    public override init() {
        
    }
    
    public func endRefreshing() {
        mj_header?.endRefreshing()
    }
    public var initStyle:(_:EeRefreshHeader)->Void = {header in
        let style = MJRefreshComponent.Style()
        style.inset = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
        header.style = style
    }
    
    
    var mj_header:EeRefreshHeader? = nil
    public func setScrollView(scrollView: UIScrollView) {
        let header = EeRefreshHeader {[weak self] in
            self?.refreshBlock()
        }
        header.tag = UIScrollView.tag_paginator_header
        initStyle(header)
        scrollView.mj_header = header
        self.mj_header = header
    }
}


public class MoLoadFoot:NSObject,MoPageFooter{
    public func endLoadWithNoMoreData() {
        footer.status = .end
//        mj_footer?.endRefreshingWithNoMoreData()
    }
    
    public func beginLoad() {
        loadBlock()
//        mj_footer?.beginRefreshing()
    }
    
    public var loadBlock: () -> Void = {}
    
    public func endLoad() {
        footer.status = .waiting
//        print("+++++++\(footer)")
//        mj_footer?.endRefreshing()
    }
    
    
    public func resetNoMoreData() {
        footer.status = .waiting
//        mj_footer?.resetNoMoreData()
    }
    
    public override init() {
        
    }

    
    public var footer = MoFooterLoading()
    public func setScrollView(scrollView: UIScrollView) {
        footer.scrollView = scrollView
        footer.triggerLoading = {[weak self] in
            self?.loadBlock()
            
        }
    }
}


extension UIScrollView{
    fileprivate static let tag_paginator_header = 8806
    public var paginator_header:UIView?{
        get{
            return self.viewWithTag(UIScrollView.tag_paginator_header)
        }
    }
}
